package com.corpgovernment.common.utils.JsonDeserializerUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

public class JsonBooleanToInteger extends JsonDeserializer<Integer> {

    /**
     * 序列化 将Boolean转换为Integer(兼容int ->int) 不支持Ineter转Boolean
     * @return
     * @throws IOException
     */
    @Override
    public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        Integer number = null;
        switch (jsonParser.getCurrentToken().name()) {
            case "VALUE_NUMBER_INT":
                number =  jsonParser.getIntValue();
                break;
            case "VALUE_TRUE":
                number = 1;
                break;
            case "VALUE_FALSE":
                number = 0;
                break;
        }
        return number;
    }
}
